// @algorithm @lc id=446 lang=cpp 
// @title arithmetic-slices-ii-subsequence


// #define print(...)
// @test([2,4,6,8,10])=7
// @test([7,7,7,7,7])=16
class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int ans = 0;
        const size_t n = nums.size();
        vector<unordered_map<int64_t,int>> dn(n); // i:<diff, count>
        for(size_t i=0; i<n; i++){
            for(size_t j=0; j<i; j++){
                int64_t d = int64_t(nums[i])-nums[j];
                auto cnt = dn[j].count(d)?dn[j][d]:0;
                dn[i][d] += cnt + 1;
                ans += cnt;
            }
        }
        return ans;
    }
};